package com.ezbuy.common.helper;

import com.google.common.truth.Truth;

import org.junit.Test;

import java.io.File;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

public class NameHelper {

    private static final String without_postfix = "([^<>/\\\\|:\"\"\\*\\?]+)\\.\\w+$+";
    private static final String with_postfix = "([^<>/\\\\\\|:\"\"\\*\\?]+\\.\\w+$)";

    public static String getFileNameWithOutPostfix(String path) {
        Matcher m = Pattern.compile(without_postfix).matcher(path);
        if (m.find()) {
            return m.group(1);
        }
        return "";
    }

    public static String getFileNameWithPostfix(String path) {
        Matcher m = Pattern.compile(with_postfix).matcher(path);
        if (m.find()) {
            return m.group(1);
        }
        return "";
    }

    /***
     * 下划线命名 -> 小驼峰命名
     * client_no -> clientNo
     */

    public static String underlineToSmallHump(String para) {
        StringBuilder result = new StringBuilder();
        String a[] = para.split("_");
        for (String s : a) {
            if (result.length() == 0) {
                result.append(s.toLowerCase());
            } else {
                result.append(s.substring(0, 1).toUpperCase());
                result.append(s.substring(1).toLowerCase());
            }
        }
        return result.toString();
    }


    /***
     * 小驼峰命名 -> 下划线命名
     * clientNo -> client_no
     */
    public static String smallHumpToUnderline(String para) {
        StringBuilder sb = new StringBuilder(para);
        int temp = 0;//定位
        for (int i = 0; i < para.length(); i++) {
            if (Character.isUpperCase(para.charAt(i))) {
                sb.insert(i + temp, "_");
                temp += 1;
            }
        }
        return sb.toString().toLowerCase();
    }


    /***
     * 下划线命名 -> 大驼峰命名
     * client_no -> ClientNo
     */

    public static String underlineToBigHump(String para) {
        StringBuilder result = new StringBuilder();
        String a[] = para.split("_");
        for (String s : a) {
            if (result.length() == 0) {
                result.append(s.substring(0, 1).toUpperCase());
                result.append(s.substring(1).toLowerCase());
            } else {
                result.append(s.substring(0, 1).toUpperCase());
                result.append(s.substring(1).toLowerCase());
            }
        }
        return result.toString();
    }


    /***
     * 大驼峰命名 -> 下划线命名
     * ClientNo -> client_no
     */
    public static String bigHumpToUnderline(String para) {
        StringBuilder sb = new StringBuilder(para);
        int temp = 0;//定位
        for (int i = 1; i < para.length(); i++) {
            if (Character.isUpperCase(para.charAt(i))) {
                sb.insert(i + temp, "_");
                temp += 1;
            }
        }
        return sb.toString().toLowerCase();
    }


    /***
     * 大驼峰命名 -> 小驼峰命名
     * ClientNo -> client_no
     */
    public static String bigHumpToSmallHump(String para) {
        return underlineToSmallHump(bigHumpToUnderline(para));
    }

    /**
     * cart_public.proto -> CartPublic
     */
    public static String getRealName(String fileName) {
        return NameHelper.underlineToBigHump(NameHelper.getFileNameWithOutPostfix(fileName));
    }

    /***
     * 小驼峰命名 -> 大驼峰命名
     * ClientNo -> client_no
     */
    public static String smallHumpToBigHump(String para) {
        return underlineToBigHump(smallHumpToUnderline(para));
    }

    /**
     * 把一个字符串首字母大写
     */
    public static String capitalFirstChar(String str) {
        if (str == null || str.trim().equals("")) {
            return str;
        } else {
            char[] charArray = str.toCharArray();
            if (charArray[0] >= 'a' && charArray[0] <= 'z') {
                charArray[0] = (char) (charArray[0] - 32);
                return String.valueOf(charArray);
            } else {
                return str;
            }
        }
    }

    /**
     * "src/test/java/com/ezbuy/autogen/ftlgen" -> "com.ezbuy.autogen.ftlgen"
     */
    public static String getRootPackageName(String outDir) {
        String c = "/";
        String[] split = outDir.split(c);
        System.out.println(split.toString());
        int javaIndex = 2;
        for (int i = 0; i < split.length; i++) {
            if (split[i].equals("java")) {
                javaIndex = i;
            }
        }
        StringBuilder builder = new StringBuilder();
        for (int i = javaIndex + 1; i < split.length; i++) {
            builder.append(split[i]);
            if (i != split.length - 1) {
                builder.append(".");
            }
        }
        return builder.toString();
    }

    /**
     * "src/test/java/com/ezbuy/autogen/ftlgen" -> "src/test/java"
     */
    public static String getRootWithoutPackageName(String outDir) {
        String c = "/";
        String[] split = outDir.split(c);
        System.out.println(split.toString());
        int javaIndex = 2;
        for (int i = 0; i < split.length; i++) {
            if (split[i].equals("java")) {
                javaIndex = i;
            }
        }
        StringBuilder builder = new StringBuilder();
        for (int i = 0; i <= javaIndex; i++) {
            builder.append(split[i]);
            if (i != javaIndex) {
                builder.append("/");
            }
        }
        return builder.toString();
    }

    /**
     * com.ezbuy.autogen.ftlgen.address -> com/ezbuy/autogen/ftlgen/address
     */
    public static String replaceToSeparator(String packageName) {
        if (packageName != null) {
            return packageName.replace(".", File.separator);
        }
        return "";
    }

    /**
     * email -> setEmail
     */
    public static String getSetMethod(String name) {
        return "set" + StringUtils.upperFirstLetter(name);
    }

    /**
     * email -> getEmailList
     */
    public static String getGetMethod(String name) {
        return "get" + StringUtils.upperFirstLetter(name);
    }


    /**
     * email -> addAllEmail
     */
    public static String getSetMethodList(String name) {
        return "addAll" + StringUtils.upperFirstLetter(name);
    }

    /**
     * email -> getEmail
     */
    public static String getGetMethodList(String name) {
        return "get" + StringUtils.upperFirstLetter(name)+"List";
    }


    /**
     * cart_public.desc -> CartPublic
     */
    @Test
    public void testGetRealName() {
        String name = "cart_public.desc";
        String realName = NameHelper.underlineToBigHump(NameHelper.getFileNameWithOutPostfix(name));
        Truth.assertThat(realName).isEqualTo("CartPublic");
    }

    @Test
    public void testGetSetlMethod() {
        String name = "email";
        String setlMethod = NameHelper.getSetMethod(name);
        Truth.assertThat(setlMethod).isEqualTo("setEmail");
        String getMethod = NameHelper.getGetMethod(name);
        Truth.assertThat(getMethod).isEqualTo("getEmail");
    }

    @Test
    public void testGetSetlMethodList() {
        String name = "cartIds";
        String setlMethod = NameHelper.getSetMethodList(name);
        Truth.assertThat(setlMethod).isEqualTo("addAllCartIds");
        String getMethod = NameHelper.getGetMethodList(name);
        Truth.assertThat(getMethod).isEqualTo("getCartIdsList");
    }

    @Test
    public void getRootPackageName() {
        String outDir = "src/test/java/com/ezbuy/autogen/ftlgen";
        String name = NameHelper.getRootPackageName(outDir);
        Truth.assertThat(name).isEqualTo("com.ezbuy.autogen.ftlgen");
    }

    @Test
    public void getRootWithoutPackageName() {
        String outDir = "src/test/java/com/ezbuy/autogen/ftlgen";
        String name = NameHelper.getRootWithoutPackageName(outDir);
        Truth.assertThat(name).isEqualTo("src/test/java");
    }

    @Test
    public void getRootWithoutPackageName2() {
        String outDir = "src/test/java";
        String name = NameHelper.getRootWithoutPackageName(outDir);
        Truth.assertThat(name).isEqualTo("src/test/java");
    }

    @Test
    public void getRootPackageName2() {
        String outDir = "src/test/java";
        String name = NameHelper.getRootPackageName(outDir);
        Truth.assertThat(name).isEqualTo("");
    }
}
